package com.xuecheng.content.controller;

import com.xuecheng.api.dto.TeachPlanAndMediaDTO;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.model.dto.BindTeachPlanMediaDTO;
import com.xuecheng.content.model.dto.SaveOrUpdateTeachPlanDTO;
import com.xuecheng.content.model.dto.TeachPlanDTO;
import com.xuecheng.content.service.ITeachPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: 课程计划管理接口
 * @date 2024/12/21 13:17
 */
@RestController
@RequestMapping("/teach-plan")
@RequiredArgsConstructor
@Api(value = "课程计划管理接口", tags = "课程计划管理接口")
public class CourseTeachPlanController {

    private final ITeachPlanService teachPlanService; // 课程计划服务

    /**
     * 查询课程计划树形结构
     * @param courseId 课程id
     * @return 课程计划树形结构
     */
    @GetMapping("/{courseId}/tree-nodes")
    @ApiOperation("查询指定课程的课程计划树形结构")
    public List<TeachPlanDTO> queryTreeNodes(@PathVariable Long courseId) {
        return teachPlanService.queryTreeNodes(courseId);
    }

    /**
     * 添加或修改课程计划
     * @param saveOrUpdateTeachPlanDTO 添加或修改课程计划
     */
    @PostMapping
    @PreAuthorize("hasAnyAuthority('xc_teachmanager_course_plan')")
    @ApiOperation("添加或修改课程计划")
    public void saveOrUpdate(@RequestBody SaveOrUpdateTeachPlanDTO saveOrUpdateTeachPlanDTO){
        if (saveOrUpdateTeachPlanDTO.getId() == null)
            teachPlanService.save(saveOrUpdateTeachPlanDTO);
        else
            teachPlanService.update(saveOrUpdateTeachPlanDTO);
    }

    /**
     * 删除课程计划
     * @param id 课程计划id
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除课程计划")
    public void deleteById(@PathVariable Long id){
        teachPlanService.deleteById(id);
    }

    /**
     * 课程计划下移
     * @param id 课程计划id
     */
    @PostMapping("/move-down/{id}")
    @ApiOperation("课程计划下移")
    public void moveDown(@PathVariable Long id){
        teachPlanService.moveDownOrUp(id, true);
    }

    /**
     * 课程计划上移
     * @param id 课程计划id
     */
    @PostMapping("/move-up/{id}")
    @ApiOperation("课程计划上移")
    public void moveUp(@PathVariable Long id){
        teachPlanService.moveDownOrUp(id, false);
    }

    /**
     * 课程计划和媒资信息绑定
     * @param bindTeachPlanMediaDTO 课程计划和媒资信息绑定
     */
    @ApiOperation(value = "课程计划和媒资信息绑定")
    @PostMapping("/association/media")
    public void associationMedia(@RequestBody @Validated BindTeachPlanMediaDTO bindTeachPlanMediaDTO){
        teachPlanService.associationMedia(bindTeachPlanMediaDTO);
    }

    /**
     * 删除课程计划和媒资信息绑定
     * @param teachPlanId 课程计划id
     * @param mediaId 媒资文件id
     */
    @ApiOperation(value = "删除课程计划和媒资信息绑定")
    @DeleteMapping("/association/media/{teachPlanId}/{mediaId}")
    public void deleteMedia(@PathVariable Long teachPlanId, @PathVariable String mediaId){
        teachPlanService.deleteMedia(teachPlanId, mediaId);
    }

    /**
     * 查询课程计划和媒资信息
     * @param teachplanId 课程计划id
     * @return 课程计划和媒资信息
     */
    @ApiOperation(value = "查询课程计划和媒资信息绑定")
    @GetMapping("/{teachplanId}")
    public TeachPlanAndMediaDTO getTeachplanAndMedia(@PathVariable("teachplanId") Long teachplanId){
        return teachPlanService.getTeachplanMedia(teachplanId);
    }
}
